/////////////////////////////////////////////////////////////////////////////
// Name:        SettingsDialog.cpp
// Purpose:     
// Author:      yospaly
// Modified by: 
// Created:     04/01/2009 18:57:59
// RCS-ID:      
// Copyright:   Copyright (C) 2008, yospaly
// Licence:     
/////////////////////////////////////////////////////////////////////////////

// For compilers that support precompilation, includes "wx/wx.h".
#include "wx/wxprec.h"

#ifdef __BORLANDC__
#pragma hdrstop
#endif

#ifndef WX_PRECOMP
#include "wx/wx.h"
#endif

////@begin includes
#include "wx/imaglist.h"
////@end includes

#include "SettingsDialog.h"
#include "MirrorlistPanel.h"
#include "ProxyPanel.h"

#include "mirrorslst.h"
#include "mounttable.h"
#include "config.h"
#include "pkgmgr.h"

#include <wx/dir.h>

////@begin XPM images
////@end XPM images


/*!
 * SettingsDialog type definition
 */

IMPLEMENT_DYNAMIC_CLASS( SettingsDialog, wxDialog )


/*!
 * SettingsDialog event table definition
 */

BEGIN_EVENT_TABLE( SettingsDialog, wxDialog )

////@begin SettingsDialog event table entries
    EVT_BUTTON( ID_CLEAR_CACHE_BUTTON, SettingsDialog::OnCLEARCACHEBUTTONClick )

    EVT_BUTTON( wxID_OK, SettingsDialog::OnOKClick )

    EVT_BUTTON( wxID_CANCEL, SettingsDialog::OnCANCELClick )

////@end SettingsDialog event table entries

END_EVENT_TABLE()


/*!
 * SettingsDialog constructors
 */

SettingsDialog::SettingsDialog()
{
    Init();
}

SettingsDialog::SettingsDialog( wxWindow* parent, wxWindowID id, const wxString& caption, const wxPoint& pos, const wxSize& size, long style )
{
    Init();
    Create(parent, id, caption, pos, size, style);
}


/*!
 * Settings creator
 */

bool SettingsDialog::Create( wxWindow* parent, wxWindowID id, const wxString& caption, const wxPoint& pos, const wxSize& size, long style )
{
////@begin SettingsDialog creation
    SetExtraStyle(wxWS_EX_BLOCK_EVENTS);
    wxDialog::Create( parent, id, caption, pos, size, style );

    CreateControls();
    if (GetSizer())
    {
        GetSizer()->SetSizeHints(this);
    }
    Centre();
////@end SettingsDialog creation
    return true;
}


/*!
 * SettingsDialog destructor
 */

SettingsDialog::~SettingsDialog()
{
////@begin SettingsDialog destruction
////@end SettingsDialog destruction
}


/*!
 * Member initialisation
 */

void SettingsDialog::Init()
{
////@begin SettingsDialog member initialisation
    p_mirrors_panel = NULL;
    p_mount_box_sizer = NULL;
    p_mount_info_static = NULL;
    p_cache_dir_static = NULL;
    p_cache_size_static = NULL;
    p_cancel_button = NULL;
////@end SettingsDialog member initialisation
}


/*!
 * Control creation for Settings
 */

void SettingsDialog::CreateControls()
{    
////@begin SettingsDialog content construction
    SettingsDialog* itemDialog1 = this;

    wxBoxSizer* itemBoxSizer2 = new wxBoxSizer(wxVERTICAL);
    itemDialog1->SetSizer(itemBoxSizer2);

    wxNotebook* itemNotebook3 = new wxNotebook( itemDialog1, ID_NOTEBOOK, wxDefaultPosition, wxDefaultSize, wxBK_DEFAULT );

    p_mirrors_panel = new MirrorlistPanel( itemNotebook3, ID_MIRRORLIST_PANEL, wxDefaultPosition, wxDefaultSize, wxNO_BORDER|wxTAB_TRAVERSAL );

    itemNotebook3->AddPage(p_mirrors_panel, _("Mirrors"));

    wxPanel* itemPanel5 = new wxPanel( itemNotebook3, ID_PANEL2, wxDefaultPosition, wxDefaultSize, wxNO_BORDER|wxTAB_TRAVERSAL );
    wxBoxSizer* itemBoxSizer6 = new wxBoxSizer(wxVERTICAL);
    itemPanel5->SetSizer(itemBoxSizer6);

    wxStaticBox* itemStaticBoxSizer7Static = new wxStaticBox(itemPanel5, wxID_ANY, _("Mount Table"));
    p_mount_box_sizer = new wxStaticBoxSizer(itemStaticBoxSizer7Static, wxVERTICAL);
    itemBoxSizer6->Add(p_mount_box_sizer, 0, wxGROW|wxALL, 5);
    p_mount_info_static = new wxStaticText( itemPanel5, wxID_STATIC, _("Static text"), wxDefaultPosition, wxDefaultSize, 0 );
    p_mount_box_sizer->Add(p_mount_info_static, 1, wxGROW|wxALL, 5);

    wxStaticBox* itemStaticBoxSizer9Static = new wxStaticBox(itemPanel5, wxID_ANY, _("Download Cache"));
    wxStaticBoxSizer* itemStaticBoxSizer9 = new wxStaticBoxSizer(itemStaticBoxSizer9Static, wxVERTICAL);
    itemBoxSizer6->Add(itemStaticBoxSizer9, 0, wxGROW|wxALL, 5);
    wxFlexGridSizer* itemFlexGridSizer10 = new wxFlexGridSizer(2, 2, 0, 0);
    itemStaticBoxSizer9->Add(itemFlexGridSizer10, 0, wxALIGN_LEFT|wxALL, 5);
    wxStaticText* itemStaticText11 = new wxStaticText( itemPanel5, wxID_STATIC, _("Cache directory : "), wxDefaultPosition, wxDefaultSize, 0 );
    itemFlexGridSizer10->Add(itemStaticText11, 0, wxALIGN_RIGHT|wxALIGN_CENTER_VERTICAL|wxALL, 5);

    p_cache_dir_static = new wxStaticText( itemPanel5, wxID_STATIC, wxEmptyString, wxDefaultPosition, wxDefaultSize, 0 );
    itemFlexGridSizer10->Add(p_cache_dir_static, 0, wxALIGN_LEFT|wxALIGN_CENTER_VERTICAL|wxALL, 5);

    wxStaticText* itemStaticText13 = new wxStaticText( itemPanel5, wxID_STATIC, _("Total Size : "), wxDefaultPosition, wxDefaultSize, 0 );
    itemFlexGridSizer10->Add(itemStaticText13, 0, wxALIGN_RIGHT|wxALIGN_CENTER_VERTICAL|wxALL, 5);

    p_cache_size_static = new wxStaticText( itemPanel5, wxID_STATIC, wxEmptyString, wxDefaultPosition, wxDefaultSize, 0 );
    itemFlexGridSizer10->Add(p_cache_size_static, 0, wxALIGN_LEFT|wxALIGN_CENTER_VERTICAL|wxALL, 5);

    wxButton* itemButton15 = new wxButton( itemPanel5, ID_CLEAR_CACHE_BUTTON, _("Clear Cache"), wxDefaultPosition, wxDefaultSize, 0 );
    itemStaticBoxSizer9->Add(itemButton15, 0, wxALIGN_LEFT|wxALL, 5);

    itemNotebook3->AddPage(itemPanel5, _("Information"));

    itemBoxSizer2->Add(itemNotebook3, 0, wxALIGN_CENTER_HORIZONTAL|wxALL, 5);

    wxStdDialogButtonSizer* itemStdDialogButtonSizer16 = new wxStdDialogButtonSizer;

    itemBoxSizer2->Add(itemStdDialogButtonSizer16, 0, wxALIGN_RIGHT|wxALL, 5);
    wxButton* itemButton17 = new wxButton( itemDialog1, wxID_OK, _("&OK"), wxDefaultPosition, wxDefaultSize, 0 );
    itemStdDialogButtonSizer16->AddButton(itemButton17);

    p_cancel_button = new wxButton( itemDialog1, wxID_CANCEL, _("&Cancel"), wxDefaultPosition, wxDefaultSize, 0 );
    itemStdDialogButtonSizer16->AddButton(p_cancel_button);

    itemStdDialogButtonSizer16->Realize();

////@end SettingsDialog content construction
    
    // Show mount table info
    MountTable& mount_table = MountTable::get();
    MountPointHash mount_hash = mount_table.mount_hash;
    
    wxString info;
    MountPointHash::iterator it;
    for( it = mount_hash.begin(); it != mount_hash.end(); ++it ) {
    	info.Append(wxString::Format(_T("%s\t\t%s\n"), it->first.c_str(), it->second.native.c_str()));
    }
    
    p_mount_info_static->SetLabel(info);
    
    const wxString& cache_dir = PkgMgr::get().get_download_cache_dir();
    p_cache_dir_static->SetLabel(cache_dir);

    double cache_size = wxDir::GetTotalSize(cache_dir).ToDouble();
    const wxString& size_str = format_size(cache_size);
    p_cache_size_static->SetLabel(wxString::Format(_T("%s Bytes"), size_str.c_str()));
}


/*!
 * Should we show tooltips?
 */

bool SettingsDialog::ShowToolTips()
{
    return true;
}

/*!
 * Get bitmap resources
 */

wxBitmap SettingsDialog::GetBitmapResource( const wxString& name )
{
    // Bitmap retrieval
////@begin SettingsDialog bitmap retrieval
    wxUnusedVar(name);
    return wxNullBitmap;
////@end SettingsDialog bitmap retrieval
}

/*!
 * Get icon resources
 */

wxIcon SettingsDialog::GetIconResource( const wxString& name )
{
    // Icon retrieval
////@begin SettingsDialog icon retrieval
    wxUnusedVar(name);
    return wxNullIcon;
////@end SettingsDialog icon retrieval
}


/*!
 * wxEVT_COMMAND_BUTTON_CLICKED event handler for wxID_OK
 */

void SettingsDialog::OnOKClick( wxCommandEvent& event )
{
	event.Skip();
	
	Config& config = Config::get();
    
    if ( p_mirrors_panel->p_mirror_text->GetValue().IsEmpty() ) {
        p_mirrors_panel->p_location_static->SetForegroundColour(*wxRED);
        p_mirrors_panel->p_location_static->Refresh();
        p_mirrors_panel->p_mirror_text->SetFocus();
    	
    	wxMessageBox(_("Mirror location can't be empty!"),
    			_("Error"), wxICON_ERROR);
    	event.Skip(false);
    } else {
        config.set_last_mirror(p_mirrors_panel->p_mirror_text->GetValue());
    }
}


/*!
 * wxEVT_COMMAND_BUTTON_CLICKED event handler for wxID_CANCEL
 */

void SettingsDialog::OnCANCELClick( wxCommandEvent& event )
{
	event.Skip();
	
	Config& config = Config::get();

	if ( config.get_last_mirror().IsEmpty() ) {
        p_mirrors_panel->p_location_static->SetForegroundColour(*wxRED);
        p_mirrors_panel->p_location_static->Refresh();
        p_mirrors_panel->p_mirror_text->SetFocus();
		p_cancel_button->Disable(); // mirror location is a mandatory
		
    	wxMessageBox(_("You must select or input a mirror location!"),
    			_("Error"));
    	event.Skip(false);
	}
}


void SettingsDialog::OnCLEARCACHEBUTTONClick( wxCommandEvent& event )
{
    /* const wxString& cache_dir = PkgMgr::get().get_download_cache_dir();
    wxRmdir(cache_dir);

    double cache_size = wxDir::GetTotalSize(cache_dir).ToDouble();
    const wxString& size_str = format_size(cache_size);
    p_cache_size_static->SetLabel(wxString::Format(_T("%s Bytes"), size_str.c_str()));
    */

    event.Skip();
    wxMessageBox(_("Not yet implemented"), _("Sorry"), wxOK);
}
